package com.company.demo01InetAddress;
import java.net.InetAddress;


public class Example {
    public static void main(String[] args) {
        try {
            // 获取本地主机对象
            InetAddress localIp = InetAddress.getLocalHost();
            System.out.println("本地主机地址：" + localIp);
            System.out.println("主机名：" + localIp.getHostName());
            System.out.println("IP地址：" + localIp.getHostAddress());

            // 获取网络主机对象
            InetAddress netIp = InetAddress.getByName("www.baidu.com");
            System.out.println("网络主机地址：" + netIp);
            System.out.println("主机名：" + netIp.getHostName());
            System.out.println("IP地址：" + netIp.getHostAddress());

            // 判断本机与网络主机是否能连通
            if(localIp.isReachable(3000)){
                System.out.println("本机与网络主机连通");
            }
            else{
                System.out.println("本机与网络主机不连通");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
